﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.ReturnOrder;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.ReturnOrder
{
    public class OrderReturnServices : IOrderReturnServices
    {
        public OrderReturnServices(IOrderReturnRepository or, IBaseRepository db)
        {
            Or = or;
            Db = db;
        }

        public IOrderReturnRepository Or { get; }
        public IBaseRepository Db { get; }

        public async  Task<bool> AddAsync(OrderReturn model, List<OrderReturnItem> list)
        {
            var result = await Or.AddOrderReturn(model, list);
            return result;
        }

        public async  Task<bool> DeleteAsync(int[] Ids)
        {
            var result = await Or.DeleteOrderReturn( Ids);
            return result;
        }

        public async  Task<IEnumerable<OrderReturn>> GetAllAsync()
        {
            string sql = "select * from OrderReturn";
            var list = await Db.QueryAllAsync<OrderReturn>(sql);
            return list;
        }

        public async  Task<IEnumerable<OrderReturn>> GetAsync(int PageIndex, int PageSize, string Number, string CustomerNumber, string WarehouseId, string OrderSalesExWarehouseId)
        {
            string sql = "select * from OrderReturn where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(CustomerNumber))
            {
                sql += " and CustomerNumber like @CustomerNumber ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(OrderSalesExWarehouseId))
            {
                sql += " and OrderSalesExWarehouseId like @OrderSalesExWarehouseId ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await Db.QueryAsync<OrderReturn>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", CustomerNumber = $"%{CustomerNumber}%",  WarehouseId = $"%{WarehouseId}%", OrderSalesExWarehouseId = $"%{OrderSalesExWarehouseId}%" });
            return list;
        }

        public async  Task<int> GetCountAsync(string Number, string CustomerNumber, string WarehouseId, string OrderSalesExWarehouseId)
        {
            string sql = "select count(*) from OrderReturn where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(CustomerNumber))
            {
                sql += " and CustomerNumber like @CustomerNumber ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (!string.IsNullOrEmpty(OrderSalesExWarehouseId))
            {
                sql += " and OrderSalesExWarehouseId like @OrderSalesExWarehouseId ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", CustomerNumber = $"%{CustomerNumber}%",  WarehouseId = $"%{WarehouseId}%", OrderSalesExWarehouseId = $"%{OrderSalesExWarehouseId}%" });
            return count;
        }

        public async Task<IEnumerable<OrderReturnItemDto>> GetOrderReturnbyIdAsync(string OrderReturnId, string WareHouseId)
        {
            var list = await Db.QueryAsync<OrderReturnItemDto>(@"
select item.Spec,item.ItemName,item.ItemCode,item.UnitOfMeasure,(ik.StockCount-ik.SalesCount) as CurrCount,oq.* from OrderReturnItem oq
inner join Item item on oq.ItemId = item.Id inner join ItemStock ik on ik.ItemId=item.Id and ik.WareHouseId=@WarehouseId where oq.OrderReturnId = @OrderReturnId", new { OrderReturnId, WareHouseId });


            
            return list;
        }

        public async  Task<bool> UpdateAsync(OrderReturn model, List<OrderReturnItem> list)
        {
            var result = await Or.UpdateOrderReturn(model, list);
            return result;
        }

        public async Task<bool> UpdatStatus(long Id, int Status)
        {
            var sql = "update OrderReturn set Status=@Status where Id =@Id";

            var result = await Db.UpdateAsync(sql, new { Id, Status });

            return result;
        }
    }
}
